package leetcode_301_400;

public class LeeCode_400 {
    public static void main(String[] args) {
        System.out.println(findNthDigit(2147483647
        ));
    }
    private static int findNthDigit(int n) {
        if (n == 1000000000)
            return 1;
        if (n < 10){
            return n;
        }
        long tmp = 9;
        long i = 1;
        while (n > tmp * i){
            n -= tmp * i;
            tmp *= 10;
            i++;
        }
        long count = n / i;
        long last = n % i;
        int p = 1;
        while (i > 1){
            i--;
            p *= 10;
        }
        long num = p + count - 1;
        String s = "";
        if (last == 0){
           s = Integer.toString((int) num);
            return s.charAt(s.length() - 1) - '0';
        }else {
            s = Integer.toString((int) (num + 1));
            return s.charAt((int) (last - 1)) - '0';
        }
    }
}
